package com.briup.cms.web.controller;


import com.briup.cms.bean.User;
import com.briup.cms.bean.extend.UserExtend;
import com.briup.cms.service.IUserService;
import com.briup.cms.utils.Message;
import com.briup.cms.utils.MessageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(description = "user接口")
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;


    @GetMapping("findAllWithRole")
    public Message findAllWithRole(){
        List<UserExtend> list=userService.findAllWithRole();
        return MessageUtil.success(list);
    }


    @ApiOperation(value = "查询")
    @GetMapping("findAll")
    public Message findAll(){
        List<User> list=userService.findAll();
        return MessageUtil.success(list);
    }

    @ApiOperation(value = "添加更新")
    @GetMapping("saveOrUpdate")
    public Message saveOrUpdate(User user){
           userService.saveOrUpdate(user);
        return MessageUtil.success(user);
    }

    @ApiOperation(value = "删除")
    @GetMapping("deleteById")
    public Message deleteById(Long id){
        userService.deleteById(id);
        return MessageUtil.success(id);
    }

    @PostMapping("login")
    public Message login(String username, String password){
        // 虚拟token
        Map<String,String> map = new HashMap<>();
        map.put("token","admintoken");

        return MessageUtil.success(map);
    }

    @GetMapping("info")
    public Message info(){
        User user = new User();
        user.setRealName("超级管理员");
        user.setPhoto("http://121.199.29.84:8888/group1/M00/00/05/rBD-SV7-iN6Abe0NAAAXCfK1yz8375.jpg");
        return MessageUtil.success(user);
    }

    @PostMapping("logout")
    public Message logout(){
        return MessageUtil.success("退出成功");
    }
}
